package service

import (
	"context"
	"disk/disk_rpc_server/disk_pb"
	"disk/utils/mysql"
	"gorm.io/gorm"
)

func (*GrpcServer) Add(ctx context.Context, req *disk_pb.AddReq) (resp *disk_pb.AddResp, err error) {
	db := Database.Session(&gorm.Session{NewDB: true})
	var dataList []mysql.DiskList
	var resultList []*disk_pb.DiskList
	resultList = append(resultList, req.GetList())
	mysql.DiskListRpcToOrm(resultList, &dataList)
	result := db.Create(&dataList[0])

	if result.Error != nil {
		return &disk_pb.AddResp{
			Code:    1,
			Message: result.Error.Error(),
		}, nil
	}
	return &disk_pb.AddResp{
		Code:    0,
		Message: "success",
	}, nil
}
